<?php
/**
 * Created by PhpStorm.
 * User: gyq
 * Date: 2018/11/3
 * Time: 19:04
 */

namespace app\api\model;


class Product extends BaseModel
{
    protected $hidden = ['update_time', 'delete_time', 'pivot', 'create_time', 'category_id'];

    public function imgs()
    {
        return $this->hasMany('ProductImage', 'product_id', 'id');
    }

    public function properties()
    {
        return $this->hasMany('ProductProperty', 'product_id', 'id');
    }

    public function getMainImgUrlAttr($value, $data)
    {
        return $this->imgUrlPrefix($value, $data);
    }

    public static function getRecentProduct($count)
    {
        return self::order('create_time', 'desc')->limit(0, $count)->select();
    }

    public static function getProductByCategoryId($id)
    {
        return self::where('category_id', '=', $id)->select();
    }

    public static function getProductDetail($id)
    {
        return self::with([
            'imgs' => function ($query) {
                $query->with(['imgUrl'])->order('order', 'asc');
            }
        ,'properties'])->find($id);
    }
}